package com.zhz.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhz.pojo.Food;
import com.zhz.service.FoodService;
import com.zhz.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 周恒哲
 * @since 2020-04-14
 */
@Controller
@RequestMapping("/food")
@Api(tags = {"美食控制类"})
public class FoodController {

    @Autowired
    private FoodService foodService;

    @GetMapping("/findById/{id}")
    @ApiOperation("根据ID查询美食信息")
    public String findById(@ApiParam("美食ID") @PathVariable("id") Long id,Model model) {

        try {
            Food food = this.foodService.getById(id);
            model.addAttribute("food",food);
            return "food/update";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @GetMapping("/findAll")
    @ApiOperation("查询所有美食信息")
    public String findAll(Model model) {

        try {
            List<Food> list = this.foodService.list(null);
            model.addAttribute("list",list);
            return "food/list";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping("/insertFood")
    @ApiOperation("添加美食")
    public String insertFood(@ApiParam("美食对象") Food food) {

        try {
            boolean save = this.foodService.save(food);
            return "redirect:/food/findAll";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping("/updateFood")
    @ApiOperation("修改美食信息")
    public String updateFood(@ApiParam("美食对象") Food food) {
        try {
            boolean update = this.foodService.updateById(food);
            return "redirect:/food/findAll";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @GetMapping("/deleteFoodById/{id}")
    @ApiOperation("根据单个美食ID删除美食信息")
    public String deleteFoodById(@ApiParam("美食ID") @PathVariable("id") Long id) {

        try {
            boolean remove = this.foodService.removeById(id);
            return "redirect:/food/findAll";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @GetMapping("/deleteFoodByIds")
    @ApiOperation("根据多个ID删除美食信息")
    public Result deleteFoodByIds(@ApiParam("美食ID") @RequestParam Long[] ids) {

        try {
            boolean remove = this.foodService.removeByIds(Arrays.asList(ids));
            return Result.ok(remove);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.build(500, "error");
    }
    @GetMapping("/selFoodByName")
    @ApiOperation("根据美食名字查询美食信息")
    public String selFoodByName(@ApiParam("美食名字") @RequestParam String name,Model model) {

        try {
            QueryWrapper<Food> wrapper=new QueryWrapper<>();
            wrapper.like("name",name);
            List<Food> list = this.foodService.list(wrapper);
            model.addAttribute("name",name);
            model.addAttribute("list",list);
            return "food/list";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    @GetMapping("/searchForFoodName/{name}")
    @ResponseBody
    @ApiOperation("根据美食名字查询美食信息")
    public List<Food> searchForFoodName(@ApiParam("美食名字") @PathVariable("name") String name) {
        try {
            QueryWrapper<Food> wrapper=new QueryWrapper<>();
            wrapper.like("name",name);
            List<Food> list = this.foodService.list(wrapper);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    @GetMapping("/findAllFood")
    @ApiOperation("查询所有美食信息")
    @ResponseBody
    public List<Food> findAllFood() {

        try {
            List<Food> list = this.foodService.list(null);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    @GetMapping("/findById1/{id}")
    @ApiOperation("根据ID查询美食信息")
    public String findById1(@ApiParam("美食ID") @PathVariable("id") Long id,Model model) {

        try {
            Food food = this.foodService.getById(id);
            model.addAttribute("food",food);
            return "detail";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}